/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entreprise;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mapping.DBPatient;

/**
 *
 * @author Quoc-Khai
 */
public class PatientService {
    DBPatient dBPatient;
    public Patient getPatient(String patientID){
        if(patientID.equals(""))return null;
        DBPatient dBPatient=new DBPatient();
        Patient patient=null;        
        try {
            patient=dBPatient.getPatient(patientID);
        } catch (SQLException ex) {
            Logger.getLogger(PatientService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return patient;        
    }
    public boolean createPatient(String patientID, String patientName, String patientSex, Date patientDateOfBirth, String patientJob, String patientAddress, String patientPhoneNumber, String patientEmail, float patientHeight, float patientWeight, String patientBloodType){
        DBPatient dbPatient = new DBPatient();
        Patient iPatient = new Patient();
        iPatient.setPatientName(patientName);
        iPatient.setPatientSex(patientSex);
        iPatient.setPatientDateOfBirth(patientDateOfBirth);
        iPatient.setPatientJob(patientJob);
        iPatient.setPatientAddress(patientAddress);
        iPatient.setPatientPhoneNumber(patientPhoneNumber);
        iPatient.setPatientEmail(patientEmail);
        iPatient.setPatientHeight(patientHeight);
        iPatient.setPatientWeight(patientWeight);
        iPatient.setPatientBloodType(patientBloodType);
        if(dbPatient.createPatient(iPatient)){
            return true;
        }else{
            return false;
        }     
    }
    public boolean deletePatient(String patientID){ 
        DBPatient dbPatient = new DBPatient();
        Patient iPatient = new Patient();
        try {
            iPatient = dbPatient.getPatient(patientID);
        } catch (SQLException ex) {
            Logger.getLogger(PatientService.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        if (iPatient.getPatientID().length() > 0) {
            dbPatient.deletePatient(patientID);
            return true;
        }        
//        System.out.println("Patient not existe!");
        return false;
    }
    public boolean updatePatient(String patientID,String patientName, String patientSex, Date patientDateOfBirth, String patientJob, String patientAddress, String patientPhoneNumber, String patientEmail, float patientHeight, float patientWeight, String patientBloodType){
        DBPatient dbPatient = new DBPatient();
        Patient iPatient = new Patient();
        try {
            iPatient = dbPatient.getPatient(patientID);
        } catch (SQLException ex) {
            Logger.getLogger(PatientService.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        if (iPatient.getPatientID().length() > 0) {
            iPatient.setPatientID(String.valueOf(patientID));
            iPatient.setPatientName(patientName);
            iPatient.setPatientSex(patientSex);
            iPatient.setPatientDateOfBirth(patientDateOfBirth);
            iPatient.setPatientJob(patientJob);
            iPatient.setPatientAddress(patientAddress);
            iPatient.setPatientPhoneNumber(patientPhoneNumber);
            iPatient.setPatientEmail(patientEmail);
            iPatient.setPatientHeight(patientHeight);
            iPatient.setPatientWeight(patientWeight);
            iPatient.setPatientBloodType(patientBloodType);
            
            dbPatient.updatePatient(iPatient);
            return true;
        }        
//        System.out.println("Patient not existe!");
        return false;
    }
    public List<Patient> findPatients(String patientName){
        List<Patient> patients=null;       
        return patients;
    }
    
    public ArrayList<Patient> patients(String patientName, int from, int numberOfRow){        
        DBPatient dBPatient= new DBPatient();
        ArrayList<Patient> patients=dBPatient.getPatients(patientName,from,numberOfRow);
        return patients;
    }
    public int getNumberOfPatients(String patientName){
        DBPatient dBPatient=new DBPatient();
        return dBPatient.getNumberOfPatients(patientName);
    }
    public int getLatestPatientID(){
        dBPatient=new DBPatient();
        return dBPatient.getLatestPatientID();
    }
}
